<?php
// src/Blogger/BlogBundle/Controller/BlogController.php

namespace Blogger\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route("/blogs")
 */
class BlogController extends Controller
{
    /**
     * @Route("/{id}",name="blog_show")
     * @Template()
     */
    public function showAction($id)
    {
        
        $em = $this->getDoctrine()->getEntityManager();

        $blog = $em->getRepository('BloggerBlogBundle:Blog')->find($id);
        
        if (!$blog) {
            throw $this->createNotFoundException('Unable to find Blog post.');
        }
        $comments = $em->getRepository('BloggerBlogBundle:Comment')
                   ->getCommentsForBlog($blog->getId());
        

        return  array(
            'blog'=> $blog,
            'comments'=>$comments
                );
    }
    
    /**
     * @Route("/edit/{id}",name="blog_edit")
     * @Template()
     */
    public function editAction($id)
    {
        
        $em = $this->getDoctrine()->getEntityManager();

        $blog = $em->getRepository('BloggerBlogBundle:Blog')->find($id);

        if (!$blog) {
            throw $this->createNotFoundException('Unable to find Blog post.');
        }
        
        $blog->setAuthor('Ezequiel Actualiza');
        
        $em->flush();
        var_dump($blog);
        return  new Response();
    }
}
